package com.chen.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.ClassDTO;
import com.chen.entity.Classes;
import com.chen.entity.Teacher;
import com.chen.mapper.ClassesMapper;
import com.chen.mapper.StudentMapper;
import com.chen.mapper.TeacherMapper;
import com.chen.service.ClassesService;
import com.chen.vo.ClassesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 班级相关服务实现
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService {

    // 学生
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 获取全部班级信息
     * @return
     */
    @Override
    public List<ClassesVO> findAllClasses() {
        return baseMapper.findAllClasses();
    }

    /**
     * 根据年级id获取班级信息
     * @param gradeId
     * @return
     */
    @Override
    public List<ClassesVO> findClassesByGradeId(Integer gradeId) {
        Assert.isTrue(ObjectUtil.isNotEmpty(gradeId), "年级id不能为空");
        return baseMapper.findClassesByGradeId(gradeId);
    }

    /**
     * 班级列表查看
     * @param gradeId
     * @param className
     * @param classId
     * @return
     */
    @Override
    public List<ClassesVO> getClasses(Integer gradeId, String className, Integer classId) {
        return baseMapper.findClassesByGradeIdAndClassNameAndClassId(gradeId, className, classId);
    }

    @Override
    public ClassesVO getClassById(Integer classId) {
        return baseMapper.findClassesByClassId(classId);
    }

    /**
     * 新增班级
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Integer saveClasses(ClassDTO dto) {
        // 班级名
        String name = dto.getName();
        Assert.isTrue(ObjectUtil.isNotEmpty(name), "班级名称不能为空");
        // 年级id
        Integer gradeId = dto.getGradeId();
        Assert.isTrue(ObjectUtil.isNotEmpty(gradeId), "年级不能为空");
        // 班主任id
        Integer teacherId = dto.getTeacherId();
        Assert.isTrue(ObjectUtil.isNotEmpty(teacherId), "班主任不能为空");
        Classes classes = Classes.builder().name(name).gradeId(gradeId).teacherId(teacherId).build();
        // 将教师修改为班主任
        Teacher teacher = teacherMapper.selectById(teacherId);
        if (ObjectUtil.isNotEmpty(teacher)) {
            teacher.setIsWork(1);
            teacherMapper.updateById(teacher);
        }
        return baseMapper.insert(classes);
    }

    /**
     * 修改班级信息
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Integer updateClasses(ClassDTO dto) {
        // 班级id
        Integer id = dto.getId();
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "班级id不能为空");
        // 班级名
        String name = dto.getName();
        Assert.isTrue(ObjectUtil.isNotEmpty(name), "班级名称不能为空");
        // 年级id
        Integer gradeId = dto.getGradeId();
        Assert.isTrue(ObjectUtil.isNotEmpty(gradeId), "年级不能为空");
        // 班主任id
        Integer teacherId = dto.getTeacherId();
        Assert.isTrue(ObjectUtil.isNotEmpty(teacherId), "班主任不能为空");
        Classes classes = Classes.builder().id(id).name(name).gradeId(gradeId).teacherId(teacherId).build();

        // 原本教师就不是班主任了
        Classes yclasses = baseMapper.selectById(id);
        if (ObjectUtil.isNotEmpty(yclasses)) {
            Teacher teacher = teacherMapper.selectById(yclasses.getTeacherId());
            if (ObjectUtil.isNotEmpty(teacher)) {
                teacher.setIsWork(0);
                teacherMapper.updateById(teacher);
            }

        }

        // 将教师修改为班主任
        Teacher teacher = teacherMapper.selectById(teacherId);
        if (ObjectUtil.isNotEmpty(teacher)) {
            teacher.setIsWork(1);
            teacherMapper.updateById(teacher);
        }

        return baseMapper.updateById(classes);
    }

    /**
     * 删除班级信息
     * @param classId
     * @return
     */
    @Override
    @Transactional
    public Integer delClass(Integer classId) {
        Assert.isTrue(ObjectUtil.isNotEmpty(classId), "班级id不能为空");
        // 该班级学生人数(班级中没有学生才可删除)
        Long studentCount = studentMapper.findCountByClassesId(classId);
        if (studentCount == 0) {
            // 该教师不是班主任了
            Classes classes = baseMapper.selectById(classId);
            if (ObjectUtil.isNotEmpty(classes)) {
                Teacher teacher = teacherMapper.selectById(classes.getTeacherId());
                if (ObjectUtil.isNotEmpty(teacher)) {
                    teacher.setIsWork(0);
                    teacherMapper.updateById(teacher);
                }
            }
            return baseMapper.deleteById(classId);
        }
        return 0;
    }

    @Override
    public Map<String, Object> getStudentCountForClass(Integer gradeId) {
        return baseMapper.getStudentCountForClass(gradeId);
    }

    @Override
    public ClassesVO getClassByTeacherId(Integer teacherId) {
        return baseMapper.findClassesByTeacherId(teacherId);
    }
}
